package com.yds.xutils_facrotypattern.https;

import android.content.Intent;
import android.util.Log;


import com.yds.xutils_facrotypattern.MyApplication;

import org.json.JSONObject;
import org.xutils.common.util.KeyValue;
import org.xutils.ex.HttpException;
import org.xutils.http.BaseParams;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;

import java.util.List;

/**
 * @author YDS
 * @date 2021/5/25
 * @discribe 公用资源
 */
@SuppressWarnings("all")
public class XutilsHttpBaseEngine {
    public static final int CODE_200 = 200;          //获取数据成功
    public static final int CODE_201 = 201;          //获取数据+提示
    public static final int CODE_300 = 300;          //暂无数据
    public static final int CODE_400 = 400;          //非企业用户注册
    public static final int CODE_401 = 401;          //Token失效  数据库异常
    public static final int CODE_403 = 403;          //未知用户
    public static final int CODE_500 = 500;          //Token失效
    public static final int CODE_4012 = 4012;        //其他


    /**
     * @auther 于德水
     * created at 2021/5/25 10:23
     * 方法描述：添加默认请求头
     */
    protected void addDefaultHeader(RequestParams params) {
        params.addHeader("X-Auth-Token", PreferencesCookie.getString(MyApplication.getApplication(), "TOKEN", ""));
    }

    /**
     * @auther 于德水
     * created at 2021/5/25 13:50
     * 方法描述：用于区分 code
     */
    protected void initResultCodeNormal(String result,
                                        HttpRequestCallBack callBack) {
        try {
            JSONObject object = new JSONObject(result);
            int code = object.getInt("code");
            switch (code) {
                case CODE_500:
//                    Intent intent = new Intent();
//                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    intent.setClass(MyApplication.getApplication(), LoginActivity.class);
//                    MyApplication.getApplication().startActivity(intent);
                    break;
                case CODE_200:
                    callBack.onSuccess(result);
                    break;
                case CODE_201:
                    callBack.onSuccess(result);
                    break;
                case CODE_300:
                    callBack.onSuccess(result);
                    break;
                case CODE_401:
                    callBack.onFail(object.getString("msg"));
                    break;
                default:
                    callBack.onFail(object.getString("msg"));
                    break;
            }
        } catch (Exception e) {
            callBack.onFail(e.getMessage());
            Log.i("X-utils", "initResultCodeNormal Exception = " + e.getMessage());
        }
    }

    /**
     * @auther 于德水
     * created at 2021/5/25 13:50
     * 方法描述：用于区分 code
     */
    protected void initResultCodeRestful(RequestParams params,
                                         Throwable ex,
                                         HttpRequestCallBack callBack) {
        //网络错误
        if (ex instanceof HttpException) {
            HttpException httpEx = (HttpException) ex;
            int responseCode = httpEx.getCode();
            String responseMsg = httpEx.getMessage();
            // String errorResult = httpEx.getResult();
            switch (responseCode) {
                case CODE_401:
//                    Intent intent = new Intent();
//                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    intent.setClass(MyApplication.getApplication(), LoginActivity.class);
//                    MyApplication.getApplication().startActivity(intent);
                    break;
                default:
                    callBack.onFail(responseMsg);
                    break;
            }
            printRequestLog(params, responseCode, responseMsg);
        } else { //其他错误
            callBack.onFail("其它错误");
            printRequestLog(params, -1, "其它错误");
        }
    }

    /**
     * @auther 于德水
     * created at 2021/5/25 15:55
     * 方法描述：打印请求参数日志
     */
    protected void printRequestLog(RequestParams params, String result) {
        printRequestLog(params, 0, result);
    }

    protected void printRequestLog(RequestParams params, int requestCode, String result) {
        String uri = params.getUri();
        List<BaseParams.Header> headers = params.getHeaders();
        List<KeyValue> bodyParams = params.getBodyParams();
        HttpMethod method = params.getMethod();
        String requestLog = "请求地址：" + uri
                + "\n" + "请求方法：" + method.name()
                + "\n" + "请求头：" + headers.toString()
                + (requestCode == 0 ? "" : "\n" + "请求码：" + requestCode)
                + "\n" + "请求参数：" + ((bodyParams == null || bodyParams.isEmpty()) ? params.toString() : bodyParams.toString())
                + "\n" + "请求结果：" + result;

        Log.i("X-utils", "Xutils Restful Request Log = \n" + requestLog);
    }

}
